package com.qnbc.mystery.service.impl;

import java.util.List;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.db.sql.Order;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qnbc.mystery.pojo.domain.FoodOrder;
import com.qnbc.mystery.pojo.domain.OrderDetail;
import com.qnbc.mystery.pojo.domain.ProductSwiperImage;
import com.qnbc.mystery.service.IFoodOrderService;
import com.qnbc.mystery.service.IOrderDetailService;
import com.qnbc.mystery.service.IProductSwiperImageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.qnbc.mystery.mapper.ProductMapper;
import com.qnbc.mystery.pojo.domain.FoodsProduct;
import com.qnbc.mystery.service.IProductService;

/**
 * 商品Service业务层处理
 *
 * @author guzj
 * @date 2023-12-27
 */
@Service
public class ProductServiceImpl extends ServiceImpl<ProductMapper, FoodsProduct> implements IProductService {
    @Autowired
    private ProductMapper productMapper;
    @Autowired
    private IProductSwiperImageService productSwiperImageService;
    @Autowired
    private IOrderDetailService orderDetailService;

    /**
     * 查询商品
     *
     * @param id 商品主键
     * @return 商品
     */
    @Override
    public FoodsProduct selectProductById(Long id) {
        FoodsProduct foodsProduct = productMapper.selectProductById(id);
        LambdaQueryWrapper<ProductSwiperImage> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(ProductSwiperImage::getProductid, foodsProduct.getId());
        List<ProductSwiperImage> list = productSwiperImageService.list(lambdaQueryWrapper);
        foodsProduct.setProductSwiperImageList(list);
        return foodsProduct;
    }

    /**
     * 查询商品列表
     *
     * @param foodsProduct 商品
     * @return 商品
     */
    @Override
    public List<FoodsProduct> selectProductList(FoodsProduct foodsProduct) {
        List<FoodsProduct> foodsProducts = productMapper.selectProductList(foodsProduct);
        for (FoodsProduct foodsProduct1 : foodsProducts) {
            //获取月销量
            LambdaQueryWrapper<OrderDetail> lambdaQueryWrapper = new LambdaQueryWrapper<>();
            int count = (int) orderDetailService.count(lambdaQueryWrapper.eq(OrderDetail::getGoodsid, foodsProduct1.getId()));
            foodsProduct1.setMonthSales(count);

            //获取月销量
            LambdaQueryWrapper<ProductSwiperImage> lambdaQueryWrapper1 = new LambdaQueryWrapper<>();
            List<ProductSwiperImage> list = productSwiperImageService.list(lambdaQueryWrapper1.eq(ProductSwiperImage::getProductid, foodsProduct1.getId()));
            foodsProduct1.setProductSwiperImageList(list);
        }
        return foodsProducts;
    }

    /**
     * 新增商品
     *
     * @param foodsProduct 商品
     * @return 结果
     */
    @Override
    public int insertProduct(FoodsProduct foodsProduct) {
        return productMapper.insertProduct(foodsProduct);
    }

    /**
     * 修改商品
     *
     * @param foodsProduct 商品
     * @return 结果
     */
    @Override
    public int updateProduct(FoodsProduct foodsProduct) {
        if (CollectionUtil.isNotEmpty(foodsProduct.getProductSwiperImageList())) {
            QueryWrapper<ProductSwiperImage> eq = new QueryWrapper<ProductSwiperImage>().eq("productid", foodsProduct.getId());
            productSwiperImageService.remove(eq);
            productSwiperImageService.saveBatch(foodsProduct.getProductSwiperImageList());
        }
        return productMapper.updateProduct(foodsProduct);
    }

    /**
     * 批量删除商品
     *
     * @param ids 需要删除的商品主键
     * @return 结果
     */
    @Override
    public int deleteProductByIds(Long[] ids) {
        return productMapper.deleteProductByIds(ids);
    }

    /**
     * 删除商品信息
     *
     * @param id 商品主键
     * @return 结果
     */
    @Override
    public int deleteProductById(Long id) {
        return productMapper.deleteProductById(id);
    }

    @Override
    public List<FoodsProduct> selectProductListByBox(FoodsProduct foodsProduct) {

        return productMapper.selectProductListByBox(foodsProduct);
    }
}
